package com.wq.ui.coms;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 按钮设定类
 *
 * @author wq
 * 2020/12/19 - 17:43
 */
public class BasicArtButton extends JButton {
    private static final long serialVersionUID = 140775626603273316L;

    boolean init = false;
    private boolean entered = false;
    private boolean pressed = false;

    private Color normalColor = new Color(154, 86, 42);
    private Color enterColor = new Color(135, 67, 23);
    private Color pressedColor = new Color(154, 86, 42).darker();

    private String name;


    /**
     * 设置 按钮基本属性
     */

    public BasicArtButton(String name) {
        this.name = name;

        this.setBorder(BorderFactory.createBevelBorder(1));
        this.setBorder(null);
        this.setOpaque(false);
        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                super.mouseEntered(e);
                entered = true;
                repaint();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                Graphics g = getGraphics();
                entered = false;
                repaint();
            }

            @Override
            public void mousePressed(MouseEvent e) {
                super.mousePressed(e);
                pressed = true;
                repaint();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                // TODO Auto-generated method stub
                super.mouseReleased(e);
                pressed = false;
                repaint();
            }
        });

    }


    /**
     * 组件绘制控制
     */

    @Override

    protected void paintComponent(Graphics g) {
//		super.paintComponent(g);
        g.setColor(Color.white);
        g.setFont(FontCreater.getDefalut().deriveFont(20f));

        if (entered && !pressed) {
            g.setColor(enterColor);

            g.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 10, 10);
        } else if (entered && pressed) {
            g.setColor(pressedColor);
            g.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 10, 10);
        } else if (!entered && pressed) {
            g.setColor(enterColor);
            g.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 10, 10);
        } else {
            g.setColor(normalColor);
            g.fillRoundRect(0, 0, this.getWidth(), this.getHeight(), 10, 10);
        }
        g.setColor(Color.white);

        FontMetrics fm = g.getFontMetrics();
        int strW = fm.stringWidth(name);
        int strh = fm.getHeight();

        g.drawString(name, this.getWidth() / 2 - strW / 2, this.getHeight() / 2 + strh / 4);
    }

    @Override
    protected void paintChildren(Graphics g) {
        super.paintChildren(g);
    }


    /**
     * 重新设置按钮颜色
     */

    public void setColor(Color color1, Color color2, Color color3) {
        normalColor = color1;
        pressedColor = color2;
        enterColor = color3;
    }


    /**
     * 设置按钮名称
     */

    @Override
    public void setName(String name) {
        super.setName(name);
        this.name = name;
        repaint();
    }


    /**
     * 设置特殊按钮监听器
     */

    public void addButtonListener(ActionListener al) {
        addActionListener(al);
    }

}
